package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.vo.WipMarkingElSummaryVO;
import com.xdcplus.web.mapper.WipMarkingIvCountMapper;
import com.xdcplus.web.common.pojo.entity.WipMarkingIvCount;
import com.xdcplus.web.common.pojo.dto.WipMarkingIvCountDTO;
import com.xdcplus.web.common.pojo.dto.WipMarkingIvCountFilterDTO;
import com.xdcplus.web.common.pojo.vo.WipMarkingIvCountVO;
import com.xdcplus.web.common.pojo.query.WipMarkingIvCountQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingIvCountService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * (WipMarkingIvCount)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-11-28 18:34:27
 */
@Slf4j
@Service("wipMarkingIvCountService")
public class WipMarkingIvCountServiceImpl extends BaseServiceImpl<WipMarkingIvCountMapper, WipMarkingIvCount, WipMarkingIvCount, WipMarkingIvCountVO> implements WipMarkingIvCountService {

    @Autowired
    protected WipMarkingIvCountMapper wipMarkingIvCountMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingIvCount(WipMarkingIvCountDTO wipMarkingIvCountDTO) {

        WipMarkingIvCount wipMarkingIvCount = new WipMarkingIvCount();
        BeanUtil.copyProperties(wipMarkingIvCountDTO, wipMarkingIvCount);
        wipMarkingIvCount.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(wipMarkingIvCount);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingIvCount(WipMarkingIvCountDTO wipMarkingIvCountDTO) {

        WipMarkingIvCount wipMarkingIvCount = this.getById(wipMarkingIvCountDTO.getId());
        if (ObjectUtil.isNull(wipMarkingIvCount)) {
            log.error("updateWipMarkingIvCount() The WipMarkingIvCount does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingIvCountDTO, wipMarkingIvCount);
        wipMarkingIvCount.setUpdatedUser(wipMarkingIvCountDTO.getUpdatedUser());
        wipMarkingIvCount.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingIvCount);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingIvCount> wipMarkingIvCountList) {

        wipMarkingIvCountList.forEach(wipMarkingIvCount -> {
            WipMarkingIvCount wipMarkingIvCountParam = new WipMarkingIvCount();
            wipMarkingIvCountParam.setId(wipMarkingIvCount.getId());
            if (ObjectUtil.isNotNull(wipMarkingIvCount.getId())) {
                wipMarkingIvCount.setId(wipMarkingIvCount.getId());
                wipMarkingIvCount.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingIvCount> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingIvCount::getId, wipMarkingIvCount.getId());
                update(wipMarkingIvCount, lambdaUpdate);
            } else {
                wipMarkingIvCount.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingIvCount);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingIvCountDTO> wipMarkingIvCountDTOList) {

        List<WipMarkingIvCount> wipMarkingIvCountList = BeanUtils.copyProperties(wipMarkingIvCountDTOList, WipMarkingIvCount.class);
        return saveOrUpdateBatch(wipMarkingIvCountList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingIvCountLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingIvCount wipMarkingIvCount = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingIvCount)) {
            log.error("deleteWipMarkingIvCount() The WipMarkingIvCount does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingIvCountByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingIvCount wipMarkingIvCount = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingIvCount)) {
				log.error("deleteWipMarkingIvCount() The WipMarkingIvCount does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingIvCount> queryWipMarkingIvCountList(WipMarkingIvCountFilterDTO wipMarkingIvCountFilterDTO) {
        WipMarkingIvCountQuery wipMarkingIvCountQuery = BeanUtil.copyProperties(wipMarkingIvCountFilterDTO, WipMarkingIvCountQuery.class);

        return wipMarkingIvCountMapper.queryWipMarkingIvCount(wipMarkingIvCountQuery);
    }

    @Override
    public List<WipMarkingIvCountVO> queryWipMarkingIvCountVOList(WipMarkingIvCountFilterDTO wipMarkingIvCountFilterDTO) {
        return this.objectConversion(queryWipMarkingIvCountList(wipMarkingIvCountFilterDTO));
    }

    @Override
    public PageVO<WipMarkingIvCountVO> queryWipMarkingIvCount(WipMarkingIvCountFilterDTO wipMarkingIvCountFilterDTO) {
        PageVO<WipMarkingIvCountVO> pageVO = new PageVO<>();

        if (wipMarkingIvCountFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingIvCountFilterDTO.getCurrentPage(), wipMarkingIvCountFilterDTO.getPageSize(),
                    wipMarkingIvCountFilterDTO.getOrderType(), wipMarkingIvCountFilterDTO.getOrderField());
        }

        List<WipMarkingIvCount> wipMarkingIvCountList = queryWipMarkingIvCountList(wipMarkingIvCountFilterDTO);

        PageInfo<WipMarkingIvCount> pageInfo = new PageInfo<>(wipMarkingIvCountList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingIvCountList));

        return pageVO;
    }

    @Override
    public WipMarkingIvCountVO queryWipMarkingIvCountById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<WipMarkingIvCountVO> queryByMarkingId(Long markingId) {
        return this.objectConversion(wipMarkingIvCountMapper.queryByMarkingId(markingId));
    }
}
